package com.buba.manifest.controller;

import com.buba.customer.pojo.Customer;
import com.buba.customer.service.CustomerService;
import com.buba.manifest.pojo.OrderForm;
import com.buba.manifest.service.OrderFormService;
import com.buba.matter.pojo.Info;
import com.buba.matter.service.InfoService;
import com.buba.matter.service.PriceService;
import com.buba.system.pojo.Employee;
import com.buba.system.pojo.Job;
import com.buba.system.pojo.Page;
import com.buba.util.GenerateKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/OrderForm")
public class OrderFormController {
    private OrderFormService orderFormService;
    private PriceService priceService;
    /**
     * 查询售货订单信息
     * @param map
     * @return
     */
    @RequestMapping("/selOrderFormTable")
    public Page selOrderFormTable(@RequestParam Map<String, Object> map){
        Page<OrderForm> page = new Page<>(Integer.parseInt(map.get("pageNow").toString()));
        map.put("page", page);
        String revCurrencyModeNo = map.get("revCurrencyModeNo").toString();

        List<OrderForm> list = null;
        // 没有条件查询
        if ("".equals(revCurrencyModeNo) ) {
            //查询售货订单总条数
            page.computePageTotal(orderFormService.selOrderFormCount());
            //查询所有售货订单
            list = orderFormService.selOrderFormTable(page);
        }else { // 携带查询条件
            page.computePageTotal(orderFormService.selCount(map));
            list = orderFormService.selAllOrderForm(map);
        }
        //把list装入page类
        page.setList(list);
        return page;
    }
    /**
     * 删除收货单信息
     * @param
     * @return
     */
    @RequestMapping("/delOrderFormAll")
    public Map<String,Object> delOrderFormAll(String ids){
        Map<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        Integer delOrderForm = orderFormService.delOrderFormAll(list);

        if (delOrderForm != null){
            map.put("message","删除"+delOrderForm+"条数据");
        }else{
            map.put("message","删除失败");
        }
        return map;
    }

    /**
     * 添加收货单信息
     * @RequestParam
     * @return
     */
    @RequestMapping("/addOrderForm")
    public Map<String,Object> addOrderForm(@RequestParam Map<String, Object> map){
        //指定格式开头加上当前的事件作为id 调用时间戳类
        String xhOrderNo = GenerateKey.getKey("OF");
        map.put("xhOrderNo",xhOrderNo);

        //物质净高价（订货单价 - 物质价格）+
        Integer con= priceService.selMatterPriceByNo(map);
        Integer s= Integer.parseInt(map.get("itemsPrice").toString());
        Integer itemsProfitPrice=s-con;
        map.put("itemsProfitPrice",itemsProfitPrice);
        //1、添加收货单信息
        Integer count = orderFormService.addOrderForm(map);

        if(count>0){
            map.put("message","添加成功");
        }else {
            map.put("message","添加失败");
        }
        return map;
    }

    /**
     * 修改收货单信息
     * @param
     * @return
     */
    @RequestMapping("/updOrderForm")
    public Map<String, Object> updOrderForm(OrderForm orderForm) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderForm",orderForm);
        Integer updOrderFormCount = orderFormService.updOrderForm(map);
        return map;
    }



    /**--------------------------------------------------------------------
     * 鉴别登录用户是否拥有业务员权限
     * @param session
     * @throws Exception
     */
    @RequestMapping("/identificationSalesman")
    public Map<String,Object> identificationSalesman(HttpSession session) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("message","");
        Employee employee = (Employee)session.getAttribute("employee");
        List<Job> jobList = employee.getJobList();
        for (Job job:jobList) {
            if (job.getJobId()==1){
                map.put("message","成功");
            }
        }
        return map;
    }

    /**
     * 鉴别登录用户是否拥有财务员权限
     * @param session
     * @throws Exception
     */
    @RequestMapping("/identifyFinancialOfficer")
    public Map<String,Object> identifyFinancialOfficer(HttpSession session) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("message","");
        Employee employee = (Employee)session.getAttribute("employee");
        List<Job> jobList = employee.getJobList();
        for (Job job:jobList) {
            if (job.getJobId()==3){
                map.put("message","成功");
            }
        }
        return map;
    }



    /**
     * 财务员批量未通过订单方法
     *
     * @param ids
     * @return
     */
    @RequestMapping("/batchNoPass")
    public Map<String, Object> batchNoPass(String ids) {
        Map<String, Object> map = new HashMap<>();
        //切割选中的id字符串
        String[] split = ids.split(",");
        //装进list集合
        List<String> list = Arrays.asList(split);
        //删除客户信息表
        Integer count = orderFormService.batchNoPass(list);
        if (count != null) {
            map.put("message", "批量未通过成功！");
        } else {
            map.put("message", "批量未通过失败！");
        }
        return map;
    }





    /**
     * 财务员批量通过订单方法
     *
     * @param ids
     * @return
     */
    @RequestMapping("/batchPass")
    public Map<String, Object> batchPass(String ids) {
        Map<String, Object> map = new HashMap<>();
        //切割选中的id字符串
        String[] split = ids.split(",");
        //装进list集合
        List<String> list = Arrays.asList(split);
        //删除客户信息表
        Integer count = orderFormService.batchPass(list);
        if (count != null) {
            map.put("message", "批量通过成功！");
        } else {
            map.put("message", "批量通过失败！");
        }
        return map;
    }



    /**
     * 财务员通过单个订单的方法  可携带备注信息
     * @param map
     * @return
     */
    @RequestMapping("/pass")
    public Map<String, Object> pass(@RequestParam Map<String, Object> map) {
        //把获取到的数据进行修改  返回几条数据
        Long count = orderFormService.pass(map);
        if (count != 0) {
            map.put("message", "通过成功");
        } else {
            map.put("message", "通过失败");
        }
        return map;
    }


    /**
     * 财务员未通过单个订单的方法  可携带备注信息
     * @param map
     * @return
     */
    @RequestMapping("/noPass")
    public Map<String, Object> noPass(@RequestParam Map<String, Object> map) {
        //把获取到的数据进行修改  返回几条数据
        Long count = orderFormService.noPass(map);
        if (count != 0) {
            map.put("message", "未通过成功");
        } else {
            map.put("message", "未通过失败");
        }
        return map;
    }

    /**
     * 查询财务员权限下的需求单信息
     * @param
     * @return
     */
    @RequestMapping("/selOrderFormForTreasurer")
    public Page selOrderFormForTreasurer(@RequestParam Map<String, Object> map) {
        Page<OrderForm> page = new Page<>(Integer.parseInt(map.get("pageNow").toString()));
        map.put("page", page);
        List<OrderForm> list = null;
        //查询需求单信息数量
        Long count = orderFormService.selOrderFormCountForTreasurer(map);
        page.setCount(count);
        //查询客户信息
        List<OrderForm> OfList = orderFormService.selOrderFormForTreasurer(map);
        page.setList(OfList);
        return page;
    }


    @Autowired
    public void setPriceService(PriceService priceService) {
        this.priceService = priceService;
    }

    @Autowired
    public void setOrderFormService(OrderFormService orderFormService) {
        this.orderFormService = orderFormService;
    }
}
